(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("buyBackService", buyBackService);
	buyBackService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'arithService' ];
	function buyBackService($q, loggerFactory, commonService, ajaxServiceFactory, arithService) {
		var log = loggerFactory.getLogger("buyBackService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getCurrencyOptions : {
				url : 'do/cda/dataList/currenciesBySiteCode',
				method : 'get'
			}
		});
		
		function calTotalPayAmount(piList, deletePiList){
			var totalPayAmount = 0;
			angular.forEach(piList, function(value, key) {
				if(notInclude(value, deletePiList)){
					totalPayAmount = parseFloat(arithService.add(totalPayAmount, value.pipayAmt));
				}
			});
			return totalPayAmount;
		};
		function calTotalPiAmt(piList, deletePiList){
			var totalPiAmt = 0;
			angular.forEach(piList, function(value, key) {
				if(notInclude(value, deletePiList)){
					totalPiAmt = parseFloat(arithService.add(totalPiAmt, value.piOutsAmt));
				}
				
			});
			return totalPiAmt;
		};
		
		function notInclude(pi, deletePiList){
			if(deletePiList && deletePiList.length > 0){
				for(var i = 0; i < deletePiList.length; i++){
					if(deletePiList[i].piId == pi.piId){
						return false;
					}
				}
			}
			return true;
		}
		
		function getCurrencyOptions() {
			var defer = $q.defer();
			ajaxService.getCurrencyOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};
		
		return {
			getCurrencyOptions : getCurrencyOptions,
			calTotalPiAmt : calTotalPiAmt,
			calTotalPayAmount : calTotalPayAmount
		};
	}
})(angular);